import {Component, Input} from '@angular/core';

@Component({
  selector: ' app-carb-reduction-rank',
  templateUrl: './carb-reduction-rank.component.html',
  styleUrl: './carb-reduction-rank.component.scss'
})
export class CarbReductionRankComponent {
  pgList: number[] = [200, 180, 160, 110, 90, 60];
  mkList: IMarketList[] = [
    {
      name: "市场调度次数",
      price: 25,
      unit: "次"
    },
    {
      name: "市场调度电量",
      price: 40,
      unit: "kWh"
    },
    {
      name: "市场调度成本",
      price: 250,
      unit: "元"
    },
    {
      name: "市场调度收益",
      price: 200,
      unit: "元"
    }
  ]
  dates: string[] = this.getYearMonth(new Date());
  datesIndex: number = this.dates.length - 1;
  pgListMap: { [key: number]: number[] } = {}
  mkListMap: { [key: number]: number[] } = {}

  constructor() {
    const lastDates = this.dates.length - 1;
    this.pgListMap[lastDates] = this.pgList.map(item => item)
    this.mkListMap[lastDates] = this.mkList.map(item => item.price)
  }

  getYearMonth(date: Date): string[] {
    const dates: string[] = [];
    const year = date.getFullYear();
    const month = `0${date.getMonth() + 1}`.slice(-2); // 月份从0开始，所以加1，然后用slice填充前导零
    for (let i = 0; i < Number(month); i++) {
      dates.push(`${year}-${(i < 10 ? '0' : '') + (i + 1)}`)
    }
    return dates;
  }

  changeDate(index: number) {
    this.datesIndex = index;
    //创建随机数改变pgList,使用for循环改变pgList[i]
    if (this.pgListMap[index] === undefined) {
      const numbers: number[] = this.pgList.map(() => Math.floor(Math.random() * 200));
      numbers.sort((a, b) => b - a)
      for (let i = 0; i < this.pgList.length; i++) {
        this.pgList[i] = numbers[i];
      }
      this.pgListMap[index] = this.pgList.map(item => item);
    } else {
      const pgList = this.pgListMap[index];
      for (let i = 0; i < pgList.length; i++) {
        this.pgList[i] = pgList[i];
      }
    }
    //创建随机数改变mkList,使用for循环改变mkList[i].price
    if (this.mkListMap[index] === undefined) {
      for (let i = 0; i < this.mkList.length; i++) {
        this.mkList[i].price = Math.floor(Math.random() * 300);
      }
      this.mkListMap[index] = this.mkList.map(item => item.price);
    } else {
      const mkList = this.mkListMap[index];
      for (let i = 0; i < mkList.length; i++) {
        this.mkList[i].price = mkList[i];
      }
    }
  }
}

interface IMarketList {
  name: string;
  price: number;
  //单位
  unit: string;
}
